var code = sessionStorage.getItem("code")

var name = sessionStorage.getItem("name")
var wxh = ''
var hd_type = ''
var hf_id=''
var id_prikey=getRequest('id')
$(function() {
	mui.init()
	$.post("selectYwByActivityType", {
		activityType: code
	}, function(data) {
		/*alert(JSON.stringify(data))*/
		if(data.code == 0) {
			var rel = data.data
			var html = '<option value="">--请选择--</option>'
			for(var i in rel) {
				html += '<option value="' + rel[i].ID_PRIKEY + '">' + rel[i].业务名称 + '</option>'
			}
			$("#blyw").html(html)
			if(id_prikey!=null){
				$("#sm").css("display","none")
				$.post("selectActivityById",{id_prikey:id_prikey},function(data){
					var rel=data.data;
					$("#blyw").val(rel.办理业务)
					$("#number").val(rel.账号1)
					$("#shdz").val(rel.客户地址)
					wxh=rel.微信号
					code=rel.活动类型
					add(rel.微信号,rel.活动类型)
				})
			}
		}
	})
	//xuanze 
	$("#blyw").change(function(){
		var value=$(this).val()
		if(value=="3"){
			$(".xyk").css("display","block")
			$(".num").css("display","none")
		}else{
			$(".xyk").css("display","none")
			$(".num").css("display","block")
		}
	})
	//核销奖品
	$(".jp").delegate('.mui-badge-primary', 'click', function() {
		
		var jpid = $(this).attr("code")
		mui.showLoading()
		$(this).css("display","none")
		if(hf_id==""){
			mui.alert("请先查询是否可以充值")
			return
		}
		
		$.post("WriteOffPrize", {
			activityType: code,
			activityNumber: hd_type,
			id_prikey: jpid,
			hfczid:hf_id
		}, function(data) {
			/*alert(JSON.stringify(data))*/
			if(data.code == 0) {
				mui.alert(data.message, function() {
					add(wxh,code)
				})
			}else{
				mui.hideLoading()
				mui.alert(data.message)
			}
		})
	})
	//核销奖品
	$(".jp").delegate('.mui-badge-purple', 'click', function() {
		var jpid = $(this).attr("code")
		$(this).css("display","none")
		var row=$(this)
		var hf=$(this).siblings(".mui-badge-primary")
		mui.showLoading()
		$.post("telcheck", {
			activityType: code,
			activityNumber: hd_type,
			id_prikey: jpid
		}, function(data) {
			// alert(JSON.stringify(data))
			if(data.code == 0) {
				mui.hideLoading()
				hf_id=data.data.id_PRIKEY
				mui.alert(data.data.selectReason,function(){
					row.css("display","none")
					hf.css("display","block")
				})
			}else if(data.code=='400'){
				mui.hideLoading()
				mui.alert(data.message)
				hf_id=data.data.id_PRIKEY
				hf.css("display","block")
			}else{
				mui.hideLoading()
				mui.alert(data.message)
				row.css("display","block")
			}
		})
	})
	//扫描客户二维码
	$("#sm").click(function() {
		dd.biz.util.scan({
			type: 'all', // type 为 all、qrCode、barCode，默认是all。
			tips: "长治漳泽农商银行", //进入扫码页面显示的自定义文案
			onSuccess: function(data) {
				wxh = data.text;
				mui.showLoading()
				add(wxh, code)

			},
			onFail: function(err) {

			}
		})
	})
	$("#tj").click(function() {
		var blyw = $("#blyw").val()
		if(blyw == "") {
			mui.alert("请选择办理的业务")
			return
		}
		var zhnumber=$("#number").val()
		if(blyw!="3"&&zhnumber==""){
			mui.alert("请输入办理业务的账号或卡号")
			return
		}
		if(zhnumber!=""){
			zhnumber = Base64.encode(zhnumber)
		}
		
		var sfzh=$("#sfzh").val()
		if(sfzh==""&&blyw=="3"){
			mui.alert("请输入客户的身份证号")
			return
		}
		if(sfzh!=""){
			sfzh=Base64.encode(sfzh)
		}
		
		var shdz=$("#shdz").val()
		if(hd_type==""){
			mui.alert("请扫描客户二维码，获取相关信息")
			return
		}
		var senddata={business:blyw,address:shdz,number1:zhnumber,activityType:code,activityNumber:hd_type,id_prikey:id_prikey,IDCard:sfzh}
		mui.showLoading()
		$.post("updateActivityUserDD",senddata,function(data){
			mui.hideLoading()
			if(data.code==0){
				mui.alert(data.message,function(){
					window.location.reload()
				})
			}else{
				mui.alert(data.message)
			}
		})

	})
})

function add(a, b) { //扫描二维码
	$.post("selectWriteOffPrizeList  ", {
		openId: a,
		activityType: b
	}, function(data) {
		/*alert(JSON.stringify(data))*/
		if(data.code == 0) {
			var rel = data.data[0];
			$(".name").text(rel.姓名)
			$(".tel").text(rel.手机号)
			$(".yxhm").text(rel.营销经理姓名)
			$(".time").text(rel.登记时间)
			hd_type= rel.活动号
			id_prikey=rel.id_prikey
			abb(rel.奖品列表)
		} else {
			mui.alert(data.msg)
			mui.hideLoading()
			
		}
	})
}

function abb(row) {
	/*alert(JSON.stringify(row))*/
	var html = ''
	for(var i in row) {
		if(row[i].备注 == 0) {
			html += '<li class="mui-table-view-cell">' + row[i].奖品名称;
			html += '<span class="mui-badge-purple mui-badge" code="' + row[i].id_prikey+ '" >查询能否充值</span><span class="mui-badge mui-badge-primary" code="' + row[i].id_prikey+ '" style="display:none">核销奖品</span></li>'
		} else {
			html += '<li class="mui-table-view-cell">' + row[i].奖品名称;
			html += '<span class="mui-badge-danger mui-badge-inverted" code="' + row[i].id_prikey+ '" >已核销</span></li>'
		}
	}
	$(".jp").html(html)
	mui.hideLoading()
}